﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DomainInfoLib
{
   public class DomainInfoCache
   {
       static DataSet _DS;

       static Dictionary<string, RegisterInfo> DomainInfoDC=new Dictionary<string,RegisterInfo>();

       internal static bool Contains(string DomainName)
       {
           try
           {
               DataTable dt = GetDT();
               
               var query = from q in dt.AsEnumerable()
                           where q["DomainName"].ToString() == DomainName
                           select q;


               if (query.ToList().Count>0)
               {
                   return true;
               }
               return false;
           }
           catch
           {
               return false;
           }

       }

       private static DataTable GetDT()
       {
           if (_DS == null) _DS = LoadDS();

           if (!_DS.Tables.Contains("CacheTable"))
           {

               DataTable _DT = GetNewDataTable();

               _DS.Tables.Add(_DT);
           }

           return _DS.Tables["CacheTable"];
       }

       private static DataTable GetNewDataTable()
       {
           DataTable _DT = new DataTable("CacheTable");

           _DT.Columns.Add("DomainName");

           _DT.Columns.Add("Avaliable");

           _DT.Columns.Add("XMLContent");

           _DT.Columns.Add("UpdateDate");
           return _DT;
       }

       private static DataSet LoadDS()
       {
           try
           {
               DataSet ds = new DataSet();

               ds.ReadXml(GetXmlPath());

               return ds;
           }
           catch (Exception se)
           {
               DataSet ds = new DataSet();

               ds.Tables.Add(GetNewDataTable());

               ds.WriteXml(GetXmlPath());

               return LoadDS();
               
           }

       }



       public static RegisterInfo GetCache(string DomainName)
       {
           try
           {
               DataTable dt = GetDT();

               var query = from q in dt.AsEnumerable()
                           where q["DomainName"].ToString() == DomainName
                           select q;


               if (query.ToList().Count > 0)
               {
                   RegisterInfo info = new RegisterInfo();
                   DataRow dr=query.ElementAt<DataRow>(0);
                   info.DomainName = DomainName ;
                   info.Response = dr["XMLContent"].ToString();
                   info.Avaliable = Convert.ToBoolean(dr["Avaliable"].ToString());
                   info.QueryDate = Convert.ToDateTime(dr["UpdateDate"].ToString());
                   return info;
               }
               return null;
           }
           catch
           {
               return null;
           }
       }

       static int newRowsCount = 0;

       public static void AddCache(RegisterInfo info)
       {
           try
           {
               DataTable dt = GetDT();

               DataRow dr = dt.NewRow();

               dr["DomainName"] = info.DomainName;

               dr["Avaliable"] = info.Avaliable.ToString();

               dr["XMLContent"] = info.Response ;

               dr["UpdateDate"] = info.QueryDate.ToShortDateString();

               dt.Rows.Add(dr);

               newRowsCount++;

               if (newRowsCount/200>=1)
               {
                   Save();

                   newRowsCount = 0;
               }

           }
           catch
           {
               
           }
       }
       private static string GetXmlPath()
       {
           return "D:\\DomainNameCache.Xml";
       }


       /// <summary>
       /// 保存缓存
       /// </summary>
       public static void Save()
       {
           _DS.WriteXml(GetXmlPath());
       }
   }
}
